<template>
	<view class="content">
		<view class="search-box flex-center-between">
			<view class="search-input">
				<input type="text" placeholder="请输入你需要的关键词" focus
					v-model.trim="keyword" @confirm="doSearch(false)" class="font32 color333"
					placeholder-class="placeholder-class" confirm-type="search">
				<image @tap="clear" v-show="keyword != ''" src="/static/image/delete.png" mode=""></image>
			</view>
			<view class="search-btn font32" @tap="goBack">取消</view>
		</view>
		<view class="search-keyword" @touchstart="blur">
			<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
				<view class="keyword-entry flex-center" hover-class="keyword-entry-tap" v-for="(item,index) in keywordList" :key="index">
					<view class="keyword-text" @tap="doSearch(item)">
						<rich-text :nodes="handleChar(item)"></rich-text>
					</view>
				</view>
			</scroll-view>
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block">
					<view class="keyword-list-header flex-center-between">
						<view>你可能感兴趣的</view>
						<view class="flex-center-center"  @tap="hotToggle">
							<text class="change font30 color999">换一换</text>
							<image src="/static/image/change.png"></image>
						</view>
					</view>
					<view class="keyword flex-wrap">
						<view v-for="(keyword, index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">{{ keyword }}</view>
					</view>
				</view>
				<view class="keyword-block" v-if="oldKeywordList.length > 0">
					<view class="keyword-list-header flex-center-between">
						<view>历史搜索</view>
						<view><image @tap="oldDelete" src="/static/search/delete.png"></image></view>
					</view>
					<view class="keyword flex-wrap">
						<view v-for="(keyword, index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{ keyword }}</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import Api from '@/common/api';
export default {
	data() {
		return {
			keyword: '',
			oldKeywordList: [], // 历史搜索
			hotKeywordList: [], // 你可能感兴趣
			allhotKeywordList: [], // 所有你可能感兴趣
			keywordList: [], // 搜索后列表的数据
			isShowKeywordList: false,
			
		}
	},
	onLoad() {
		this.init()
	},
	methods: {
		init() {
			this.loadOldKeyword();
			this.loadHotKeyword();
		},
		blur() {
			uni.hideKeyboard();
		},
		//加载历史搜索,自动读取本地Storage
		loadOldKeyword() {
			uni.getStorage({
				key: 'OldKeys',
				success: res => {
					var OldKeys = JSON.parse(res.data);
					this.oldKeywordList = OldKeys;
				}
			});
		},
		//加载热门搜索
		async loadHotKeyword() {
			let res = await Api.apiEsCall('get', Api.es.interest, {})
			if(res) {
				this.allhotKeywordList = res.interestes
				this.hotKeywordList = this.allhotKeywordList.length > 10 ? this.allhotKeywordList.slice(0, 10): this.allhotKeywordList
			}
		},
		//高亮关键字
		handleChar(text) {
		    if(text.indexOf(this.keyword) !== -1) {
		        var txt = '/' + this.keyword + '/g'
		        return text.replace(eval(txt), `<span class="color333 font30">${this.keyword}</span>`)
		    }else{
		        return text
		    }
		},
		//清除历史搜索
		oldDelete() {
			uni.showModal({
				content: '确定清除历史搜索记录？',
				success: res => {
					if (res.confirm) {
						console.log('用户点击确定');
						this.oldKeywordList = [];
						uni.removeStorage({
							key: 'OldKeys'
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		//换一换
		hotToggle() {
			if(this.allhotKeywordList.length > 10) {
				let arr = []
				let n = 0
				while(arr.length != 10){
					n = Math.floor(Math.random()*this.allhotKeywordList.length)
					if(arr.indexOf(n) == -1){
						arr.push(n)
					}
				}
				this.hotKeywordList = []
				arr.map(item => {
					this.hotKeywordList.push(this.allhotKeywordList[item])
				})
			}
		},
		//执行搜索
		async doSearch(text) {
			if(this.keyword == '' && !text) {
				return 
			}
			let val = text || this.keyword
			this.saveKeyword(val); //保存为历史
			uni.navigateTo({
				url: `/pages/product/list?op=search&name=${val}`
			});
		},
		//保存关键字到历史记录
		saveKeyword(keyword) {
			uni.getStorage({
				key: 'OldKeys',
				success: res => {
					console.log(res.data);
					var OldKeys = JSON.parse(res.data);
					var findIndex = OldKeys.indexOf(keyword);
					if (findIndex == -1) {
						OldKeys.unshift(keyword);
					} else {
						OldKeys.splice(findIndex, 1);
						OldKeys.unshift(keyword);
					}
					//最多10个纪录
					OldKeys.length > 10 && OldKeys.pop();
					uni.setStorage({
						key: 'OldKeys',
						data: JSON.stringify(OldKeys)
					});
					this.oldKeywordList = OldKeys; //更新历史搜索
				},
				fail: e => {
					var OldKeys = [keyword];
					uni.setStorage({
						key: 'OldKeys',
						data: JSON.stringify(OldKeys)
					});
					this.oldKeywordList = OldKeys; //更新历史搜索
				}
			});
		},
		
		// 返回
		goBack() {
			uni.navigateBack({
				delta: 1,
				animationType: 'pop-out',
				animationDuration: 200
			})
		},
		
		// 清空搜索内容
		clear() {
			this.keyword = ''
			this.keywordList = []
			this.isShowKeywordList = false
		}
		
	},
	watch: {
		async keyword(newValue) {
			if(newValue == '') {
				this.keywordList = []
				this.isShowKeywordList = false
			}else {
				let res = await Api.apiEsCall('get', Api.es.likesearch, {
					keyword: newValue
				})
				this.keywordList = []
				if(res) {
					this.keywordList = res
				}
				this.isShowKeywordList = true
			}
		}
	}
};
</script>
<style lang="scss">
page {
	background-color: #fff;
}
view {
	display: block;
}
.search-box {
	width: 100%;
	height: 88rpx;
	padding: 0 30rpx;
	.search-input {
		position: relative;
		input {
			width: 590rpx;
			height: 70rpx;
			letter-spacing: 1rpx;
			border: 0;
			border-radius: 32rpx;
			-webkit-appearance: none;
			-moz-appearance: none;
			appearance: none;
			padding: 0 76rpx 0 26rpx;
			margin: 0;
			background-color: #f5f5f5;
		}
		image {
			position: absolute;
			top: 17rpx;
			right: 20rpx;
			width: 36rpx;
			height: 36rpx;
		}
	}
	
	.placeholder-class {
		color: #9e9e9e;
	}
	
	.search-btn {
		width: 66rpx;
		height: 70rpx;
		line-height: 70rpx;
		color: #A66354;
	}
}
.search-keyword {
	width: 100%;
	height: 100%;
	.keyword-list-box {
		height: calc(100% - 88rpx);
		.keyword-entry-tap {
			background-color: #eee;
		}
		.keyword-entry {
			width: calc(100% - 30rpx);
			height: 90rpx;
			margin-left: 30rpx;
			font-size: 30rpx;
			color: #999;
			border-bottom: 1rpx solid #eee;
			.keyword-text {
				width: 100%;
				letter-spacing: 1rpx;
			}
		}
	}
	
	.keyword-box {
		height: calc(100vh - 110upx);
		.keyword-block {
			padding: 30rpx;
			.keyword-list-header {
				width: 100%;
				padding-bottom: 11rpx;
				font-size: 30rpx;
				color: #999;
				
				.change {
					margin-right: 14rpx;
					letter-spacing: 1rpx;
				}
				image {
					width: 40upx;
					height: 40upx;
					vertical-align: middle;
				}
			}
			.keyword {
				width: 100%;
				view {
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 26rpx;
					padding: 0 30rpx;
					margin: 30rpx 30rpx 0 0;
					height: 60rpx;
					font-size: 28rpx;
					background-color: #F7F7F7;
					color: #666;
				}
			}
		}
	}
}
</style>
